package com.dw.dwdzcommerce.view.mainContent;

import java.io.IOException;

import com.dw.dwdzcommerce.base.activity.BaseActivity;
import com.dw.dwdzcommerce.base.fragment.BaseFragment;
import com.dw.dwdzcommerce.base.net.BaseNet;
import com.dw.dwdzcommerce.base.net.BaseNetInterface;
import com.dw.dwdzcommerce.base.net.NetFactory;
import com.dw.dwdzcommerce.base.persenter.BasePersenter;
import com.dw.dwdzcommerce.base.persenter.BasePersenterInterface;
import com.dw.dwdzcommerce.entity.ErrorResult;
import com.dw.dwdzcommerce.tools.commons.JsonUtil;
import com.dw.dwdzcommerce.tools.commons.L;
import com.dw.dwdzcommerce.tools.commons.StringUtil;
import com.dw.dwdzcommerce.view.details.DetailsActivity;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class mainContentPersenter extends BasePersenter implements mainContentApi, BaseNetInterface {
	private mainContentPersenterInterface persenter;
	private BaseNet net;
	private static final String TAG = "TestGeneratePersenter";

	public void bind(BasePersenterInterface basePersenterInterface) {
		this.persenter = (mainContentPersenterInterface) basePersenterInterface;
		net = NetFactory.getFactory().getDefault();
		net.bind(this);
	}

	public void onDataCallback(String json) {
		persenter.onComplete();
		if (StringUtil.isEmpty(json)) {
			L.e(TAG, "json is null or empty json=[" + json + "]");
			persenter.netWorkException("网络有点不给力哦");
			return;
		}
		JsonUtil util = JsonUtil.getUtil();
		ErrorResult errorResult = util.parseJson(json, ErrorResult.class);
		String errorMsg = null;
		if (null != errorResult) {
			L.e(TAG, "result error errorResult=[" + errorMsg + "]");
			errorMsg = errorResult.getErrorMsg();
			if (!StringUtil.isEmpty(errorMsg)) {
				persenter.remoteFailMsg(errorMsg);
				return;
			}
		}
	}

	public void onFail(IOException e) {
		L.e(TAG, "onFail " + Log.getStackTraceString(e));
		if (null != persenter) {
			persenter.onComplete();
			persenter.netWorkException("网络有点不给力哦");
		}
	}

	public interface mainContentPersenterInterface extends BasePersenterInterface {
		
	}

	@Override
	public void onFootClick() {
		BaseActivity context=(BaseActivity) ((BaseFragment) persenter).getActivity();
		Intent i=new Intent(context,DetailsActivity.class);
		context.startActivity(i);
	}
}